package com.smartum.mpr.client.view;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartum.mpr.client.Authorization;
import com.smartum.mpr.client.event.LanguageChangeHandler;
import com.smartum.mpr.client.event.WindowResizeHandler;
import com.smartum.mpr.client.presenter.LoginPresenter;

import java.util.LinkedHashMap;

/**
 * Login veiw.
 *
 * @see LoginPresenter
 */
public class LoginView extends Composite implements LoginPresenter.Display {

    private final Authorization authorizationBundle = GWT.create(Authorization.class);

    private final SelectItem languageItem;
    private final TextItem usernameItem;
    private final PasswordItem passwordItem;
    private final IButton loginButton;
    private final DynamicForm form;

    public LoginView() {

        final VLayout wrapper = new VLayout();
        wrapper.setAlign(Alignment.CENTER);
        wrapper.setWidth(Window.getClientWidth());
        wrapper.setHeight(Window.getClientHeight());

        final VLayout layout = new VLayout();
        layout.setHeight(40);
        layout.setDefaultLayoutAlign(Alignment.CENTER);

        final VLayout loginPanel = new VLayout();
        loginPanel.setWidth(240);
        loginPanel.setGroupTitle(authorizationBundle.memberArea());
        loginPanel.setIsGroup(true);
        loginPanel.setStyleName("loginPanel");

        final HLayout buttonPannel = new HLayout();
        buttonPannel.setAlign(VerticalAlignment.CENTER);
        buttonPannel.setWidth(240);

        form = new DynamicForm();
        form.setLayoutAlign(Alignment.CENTER);

        usernameItem = new TextItem();
        usernameItem.setTitle(authorizationBundle.user());
        usernameItem.setRequired(true);

        passwordItem = new PasswordItem();
        passwordItem.setTitle(authorizationBundle.password());
        passwordItem.setRequired(true);

        languageItem = new SelectItem();
        languageItem.setTitle(authorizationBundle.language());
        passwordItem.setRequired(true);

        final LinkedHashMap<String, String> supportedLanguages = new LinkedHashMap<String, String>();
        supportedLanguages.put("en_EN", "English");
        supportedLanguages.put("ru_RU", "Russian");
        supportedLanguages.put("de_DE", "German");

        LinkedHashMap<String, String> valueIcons = new LinkedHashMap<String, String>();
        valueIcons.put("en_EN", "flags/en.png");
        valueIcons.put("ru_RU", "flags/ru.png");
        valueIcons.put("de_DE", "flags/de.png");

        final String languageCookie = Cookies.getCookie(LanguageChangeHandler.LANGUAGE_COOKIE);
        languageItem.setValue(languageCookie);

        languageItem.setValueMap(supportedLanguages);
        languageItem.setValueIcons(valueIcons);

        loginButton = new IButton(authorizationBundle.enter());

        buttonPannel.addMember(loginButton);

        form.setFields(new FormItem[]{usernameItem, passwordItem, languageItem});

        loginPanel.addMember(form);
        loginPanel.addMember(buttonPannel);

        layout.addMember(loginPanel);
        wrapper.addMember(layout);

        final WindowResizeHandler handler = new WindowResizeHandler(wrapper, 0, 0, false);
        Window.addResizeHandler(handler);

        initWidget(wrapper);
    }

    public SelectItem getLanguageItem() {
        return this.languageItem;
    }

    public TextItem getUsernameItem() {
        return this.usernameItem;
    }

    public PasswordItem getPasswordItem() {
        return this.passwordItem;
    }

    public IButton getLoginButton() {
        return this.loginButton;
    }

    public DynamicForm getForm() {
        return form;
    }

    public Widget getLoginWidget() {
        return this;
    }
}
